﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MediaImporter.Controllers;
using System.IO;

namespace MediaImporter.Controls
{
    public partial class SettingsPanel : UserControl
    {
        FolderSelectorController destinationSelectorController;
        FolderSelectorController sourceSelectorController;
        SubfolderController subfolderController;
        public event EventHandler ImportPressed;


        public SettingsPanel()
        {
            InitializeComponent();
            destinationSelectorController = new FolderSelectorController(destinationSelector, Configuration.DefaultDestinationFolder);
            sourceSelectorController = new FolderSelectorController(sourceSelector, Configuration.DefaultSourceFolder);
            subfolderController = new SubfolderController(subfolderSelector, DateTime.Now);
            subfolderController.ImportPressed += subfolderController_ImportPressed;
        }

        void subfolderController_ImportPressed(object sender, EventArgs e)
        {
            if (ImportPressed != null)
                ImportPressed(sender, e);
        }

        public string SourceFolder
        {
            get { return sourceSelectorController.SelectedPath; }
        }

        public string DestinationFolder
        {
            get
            {
                return string.IsNullOrWhiteSpace(subfolderController.SubfolderName)
                    ? destinationSelectorController.SelectedPath
                    : Path.Combine(destinationSelectorController.SelectedPath, subfolderController.SubfolderName);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            AllignTextSettingsControls();
        }

        void AllignTextSettingsControls()
        {
            var textControls = new List<ITextSettingControl>();
            int maxWidth = 0;
            foreach (var control in Controls)
            {
                var textControl = control as ITextSettingControl;
                if (textControl != null)
                {
                    textControls.Add(textControl);
                    if (textControl.LabelWidth > maxWidth)
                        maxWidth = textControl.LabelWidth;
                }
            }

            foreach (var control in textControls)
            {
                control.LabelWidth = maxWidth;
            }
        }
    }
}
